﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>replace()方法的应用示例</title>
</head>
<body>
    <p>单击按钮将段落中所有“JavaScript”替换成“World”：</p>
    <p id="demo">Hello JavaScript! Hello JavaScript! Hello JavaScript!</p>
    <button onclick="myFunction()">替换</button>
    <script>
String.prototype.replaceAll = function(search, replacement) {
    let target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};
function myFunction() {
	let str=document.getElementById("demo").innerHTML;
	let n=str.replaceAll("JavaScript","World");
	document.getElementById("demo").innerHTML=n;
}
    </script>
</body>
</html>